//
//  XLImageViewController.m
//  XiangLove
//
//  Created by 李传坤 on 2018/7/30.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLImageViewController.h"
#import "XLNewHomeCell.h"
#import "MJRefresh.h"
#import "XLHTTPSessionManager.h"
#import "NewCamgirlModel.h"
#import <ReactiveCocoa.h>
#import "XLPersonPageViewController.h"
#import "TQAFNetworkDownload.h"
@interface XLImageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)UICollectionView    *collectionView;
@property (nonatomic,strong)UIImageView    *bigImage;
@property (nonatomic,strong)NSMutableArray      *modelArray;
@property (nonatomic,copy  )NSString            *page;
@property (nonatomic,strong)XLUserInfoModel     *userInfoModel;

@end
@implementation XLImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"美图";
    _bigImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    UITapGestureRecognizer *gester = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(RemoveView)];
    _bigImage.userInteractionEnabled = YES;
    [_bigImage addGestureRecognizer:gester];
    _page = @"1";[self addCollectionView];
    [self requrstTalkedPeoplePage:self.page];
    // Do any additional setup after loading the view.
}
-(void)addCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 5;
    //flowLayout.headerReferenceSize = CGSizeMake(XQScreenW, 200);
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, XQScreenW, XQScreenH) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = kWhiteColor;
    [_collectionView registerClass:[XLNewHomeCell class] forCellWithReuseIdentifier:NSStringFromClass([XLNewHomeCell class])];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderHost2"];
    //[_collectionView registerClass:[XLUsersReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handelGCHeaderTableView)];
    _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(handelGCFootTableView)];
    [self.view addSubview:_collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 下拉刷新
- (void)handelGCHeaderTableView {
    self.page = @"1";
    [self requrstTalkedPeoplePage:self.page];
}
- (NSMutableArray *)modelArray
{
    if (_modelArray == nil) {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}
#pragma mark - 上拉加载
- (void)handelGCFootTableView {
    self.page = [NSString stringWithFormat:@"%d", [self.page intValue] + 1];
    [self requrstTalkedPeoplePage:self.page];
}
- (void)requrstTalkedPeoplePage:(NSString *)page
{
    //    self.userInfoModel = [[XLAccountManager shareInstance] model];
    NSDictionary *dic = @{@"pageindex":_page,
                          @"pagesize":@"16",
                          @"userId":[[XLAccountManager shareInstance]model].Id};
    [TQAFNetworkDownload downloadUserID:@"" reqType:@"http://audit.xiuquapp.com/picture" reqMethod:@"" andOtherKeyDict:dic successDownload:^(id successMessage) {
        NSLog(@"美女列表==%@",successMessage);
        [MBProgressHUD hideHUDForView:self.view];
        self.collectionView.hidden = NO;
        NSArray * listAry = [successMessage objectForKey:@"apiData"];
        if (![successMessage isKindOfClass:[NSNull class]] && listAry.count > 0) {
            [self getJsonArray:listAry];
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
        }else{
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
            [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        }
    } failureDownload:^(id failureMessage) {
        NSLog(@"%@", failureMessage);
        [MBProgressHUD hideHUDForView:self.view];
        
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshingWithNoMoreData];
    }];
   
}

- (void)getJsonArray:(NSArray *)array {
    if ([array isKindOfClass:[NSNull class]] || array.count == 0 || array == nil) {
        
    } else {
        if ([self.page isEqualToString:@"1"]) {
            [self.modelArray removeAllObjects];
        } else {
            
        }
        [self.modelArray addObjectsFromArray:array];
    }
    [self.collectionView reloadData];
}

#pragma mark - CollectionDelegate
#pragma mark -- 返回每个item的高度
//每个item的纵向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 5;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((XQScreenW-5)/2,((XQScreenW-5)/2)*5/4);
}


#pragma mark -- collectionView 的分组个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


#pragma mark -- item 的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.modelArray.count;
    
}

#pragma mark -- cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XLNewHomeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XLNewHomeCell class]) forIndexPath:indexPath];
    [cell.hostImgView sd_setImageWithURL:self.modelArray[indexPath.row]];
    cell.stateBgView.hidden = YES;
    cell.backgroundColor = XLThemeColor;
    return cell;
}

#pragma mark -- 选中某个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [_bigImage sd_setImageWithURL:self.modelArray[indexPath.row]];
    [[UIApplication sharedApplication].keyWindow addSubview:_bigImage];
    
}
- (void)RemoveView{
    [_bigImage removeFromSuperview];
}
@end
